//
//  GoodsListController.m
//  GoodsBTS
//
//  Created by onemade on 14-9-19.
//  Copyright (c) 2014年 CNPC. All rights reserved.
//

#import "GoodsListController.h"
#import "AppDelegate.h"

@interface GoodsListController ()
{
    NSMutableArray *GoodsList;
    UIAlertView *alert;
}

@end

@implementation GoodsListController

-(AppDelegate *)appDelegate
{
    return [[UIApplication sharedApplication] delegate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    GoodsList = [[self appDelegate] GoodsList];
    
    self.tableView.contentInset = UIEdgeInsetsMake(20.0f, 0.0f, 0.0f, 0.0f);
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.tableView reloadData];
    [[BeaconMonitoringService sharedInstance] stopMonitoringAllRegions];
    [[MultipeerConnectivityAssistant sharedService] stopBrowser];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [GoodsList count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"goodInfoCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"goodInfoCell"];
    }
    
    cell.textLabel.text = [[GoodsList objectAtIndex:row] objectForKey:@"title"];
    cell.detailTextLabel.text =[[GoodsList objectAtIndex:row] objectForKey:@"remark"];
    [cell setTag:[[[GoodsList objectAtIndex:row] objectForKey:@"goodid"] integerValue]];
    [cell setEditingAccessoryType:UITableViewCellAccessoryCheckmark];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 74;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *selectedCell = nil;
    
    for (int i=1; i<[GoodsList count]; i++) {
        NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];
        selectedCell = [tableView cellForRowAtIndexPath:index];
        selectedCell.accessoryType = UITableViewCellAccessoryNone;
    }
    selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
    {
        selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark){
        selectedCell.accessoryType = UITableViewCellAccessoryNone;
    }
    
//    alert = [[UIAlertView alloc] initWithTitle:@"商品发布中.."
//                                        message:@"\n"
//                                       delegate:self
//                              cancelButtonTitle:nil
//                              otherButtonTitles:nil];
//    [alert show];
//    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
////    spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
//    spinner.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
//    [alert setValue:spinner forKey:@"accessoryView"];
//    [spinner startAnimating];
    
    [[BeaconAdvertisingService sharedInstance] stopAdvertising];
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:iBeacon_UUID];
    [[BeaconAdvertisingService sharedInstance] startAdvertisingUUID:uuid major:1 minor:selectedCell.tag identifier:iBeacon_Identifier];
    [[MultipeerConnectivityAssistant sharedService] stopAdvertising];
    [[MultipeerConnectivityAssistant sharedService] startAdvertiserWithName:selectedCell.textLabel.text];
    [MultipeerConnectivityAssistant sharedService].delegate = self;
    
    
    alert = [[UIAlertView alloc] initWithTitle:@"商品发布中.." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alert setBackgroundColor:[UIColor blackColor]];
    [alert show];
    
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    
    indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
    [indicator startAnimating];
    [alert addSubview:indicator];
    [alert setValue:indicator forKey:@"accessoryView"];
    
    
    [self performSelector:@selector(close) withObject:self afterDelay:3];
}
-(void)close{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
